package com.shq.ex1;

import android.content.pm.LabeledIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class HuanDengPianActivity extends AppCompatActivity {

    private LinearLayout hdplayout = null;
    private LinearLayout hdplayout2 = null;
    private LinearLayout hdplayout3 = null;
    private ProgressBar hdpHorizonPro = null;
    private Button hdpButton = null;
    private int hdpStatus = 0;
    private TextView text = null;
    private ImageSwitcher imageSwitcher = null;
    private final int[] hdpimages= new int[]{R.mipmap.asa,R.mipmap.asb,R.mipmap.asc,
            R.mipmap.asd,R.mipmap.ase};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huan_deng_pian);
        hdpHorizonPro = (ProgressBar)findViewById(R.id.hdpHorizonPro);
        hdpButton = (Button)findViewById(R.id.hdpButton);
        hdplayout = (LinearLayout)findViewById(R.id.hdplayout);
        imageSwitcher = new ImageSwitcher(HuanDengPianActivity.this);
        hdplayout3 = new LinearLayout(HuanDengPianActivity.this);
        hdplayout3.setOrientation(LinearLayout.VERTICAL);
        hdplayout.addView(hdplayout3);
        text = new TextView(HuanDengPianActivity.this);

        //为空间ImageSwitcher添加显示效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));//淡入
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));//淡出
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(HuanDengPianActivity.this);
            }
        });
        hdpHorizonPro.setVisibility(View.GONE);
        hdpButton.setVisibility(View.GONE);
        hdplayout2 = new LinearLayout(HuanDengPianActivity.this);
        hdplayout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        hdplayout.addView(hdplayout2);
        Gallery gallery = new Gallery(HuanDengPianActivity.this);
        //创建BaseAdapter类的对象，重写其中的方法
        BaseAdapter adapter = new BaseAdapter() {
            ImageView imageView = null;
            @Override
            public int getCount() {
                return hdpimages.length;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                imageView = new ImageView(HuanDengPianActivity.this
                );//实例化Image对象
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(300,300));
                imageView.setPadding(5,0,5,0);
                imageView.setImageResource(hdpimages[position]);
                return imageView;
            }
        };

        //将Gallery与适配器关联
        gallery.setUnselectedAlpha((float)0.9);
        gallery.setGravity(Gravity.CENTER);
        gallery.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
        gallery.setAdapter(adapter);
        gallery.setSelection(hdpimages.length/2);
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                hdplayout3.removeView(imageSwitcher);
                hdplayout3.removeView(text);
                imageSwitcher.setLayoutParams(new Gallery.LayoutParams(400,400));
                imageSwitcher.setImageResource(hdpimages[position]);
                text.setGravity(Gravity.CENTER_HORIZONTAL);
                text.setText("\n\n当前是："+(position+1)+"/"+hdpimages.length);
                hdplayout3.addView(text);
                hdplayout3.addView(imageSwitcher);
            }
        });
        hdplayout2.addView(gallery);

    }
}
